﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ue.CQRS.Samples.Application.Query;
using Ue.CQRS.Samples.Application.Query.DataContracts;
using Ue.CQRS.Samples.Application.Command.Services;
using Ue.CQRS.Samples.UI.AppCode;

namespace Ue.CQRS.Samples.UI
{
    public partial class BookEdit : Ue.CQRS.Samples.UI.AppCode.PageBase
    {
        private Lazy<ActionOptions> lazyAction;
        private Lazy<Guid?> lazyId;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.lazyAction = new Lazy<ActionOptions>(() =>
            {
                var action = this.Request.QueryString["Action"];
                return action == null
                    ? ActionOptions.Add
                    : (ActionOptions)Enum.Parse(typeof(ActionOptions), action);
            });

            this.lazyId = new Lazy<Guid?>(() =>
            {
                try
                {
                    return new Guid(this.Request.QueryString["Id"]);
                }
                catch
                {
                    return null;
                }
            });

            switch (this.Action)
            {
                case ActionOptions.Delete:
                    this.Delete();
                    break;
                case ActionOptions.Edit:
                    if (!Page.IsPostBack)
                    {
                        this.EntityToFrom(Ue.CQRS.Samples.Application.Query.Services.BookService.Instance.FindBy(this.lazyId.Value.Value));
                    }
                    break;
                default:
                    break;
            }
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            switch (this.Action)
            {
                case ActionOptions.Add:
                    this.Add();
                    break;
                case ActionOptions.Edit:
                    this.Edit();
                    break;
                default:
                    break;
            }
        }

        private void Add()
        {
            BookService.Instance.Add(name.Text.Trim(),
                description.Text.Trim(),
                author.Text.Trim(),
                isbn.Text.Trim(),
                int.Parse(pageNumber.Text.Trim()),
                int.Parse(total.Text.Trim()));

            this.Alert("添加成功");
        }

        private void Edit()
        {
            BookService.Instance.Edit(new Guid(this.Request.QueryString["Id"]),
                name.Text.Trim(),
                description.Text.Trim(),
                author.Text.Trim(),
                isbn.Text.Trim(),
                int.Parse(pageNumber.Text.Trim()));

            this.Alert("编辑成功");
        }

        private void Delete()
        {
            BookService.Instance.Delete(this.lazyId.Value.Value);

            this.Alert("编辑成功");
        }

        private void EntityToFrom(BookContract bookContract)
        {
            name.Text = bookContract.Name;
            description.Text = bookContract.Description;
            author.Text = bookContract.Author;
            isbn.Text = bookContract.ISBN;
            pageNumber.Text = bookContract.Page.ToString();
            total.Text = bookContract.Total.ToString();
            total.Enabled = false;
        }

        public ActionOptions Action
        {
            get { return this.lazyAction.Value; }
        }
    }
    
}